<template>
	<view class="login">
		<view class="title">请您授权登录</view>
		<view class="box">
			<image src="../../static/img/loginLogo.png" class="logo" mode=""></image>
		</view>
		<view class="btnBox">
			<button :disabled="disabled" open-type="getUserInfo" class="loginBtn" @getuserinfo="getUserInfo">登录</button>
			<button class="nologinBtn" @tap="go">随便逛逛</button>
		</view>
		
		<!-- <view class="marginTop10 boxes" >
			<view class="iconfont icon-xuanzhong" style="color: #6894B9;"></view>
			<view class="paddingLeft5">
				<text>登录即代表您已同意</text>
				<text style="color: #6894B9;" @tap="getnav(router(`userAgreement?type=1`))">《隐私条款声明》</text>
				<text style="color: #6894B9;" @tap="getnav(router(`userAgreement?type=2`))">《注册协议》</text>
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: false,
				iv: '',
				encryptedData: '',
				code: '',
				openid: '',
				session_key:'',
			}
		},
		onLoad(option) {
			
		},
		onShow() {
			this.disabled = false;
		},
		methods: {
			go() {
				uni.switchTab({
					url: this.router(`index`)
				})
			},

			

			getUserInfo(e) {
				console.log(e)
				this.disabled = true;
				setTimeout(()=>{
					this.disabled = false;
				}, 1000);
				uni.showToast({
					title:'请稍等.....',
					icon:'none'
				})
				var _this = this
				uni.login({
					provider: 'weixin',
					success: function(res) {
						if (res.errMsg == 'login:ok') {
							let data = {
								code: res.code
							}
							_this.http('/api/user/jscode2session', 'post', data, false)
								.then((userres) => {
									// _this.session_key = res.data.session_key;
									// _this.openid = res.data.openid;
									// this.login(userres.data.openid)
									_this.http('/api/user/login', 'post',{openid:userres.data.unionid})
										.then((d) => {
											if(d.sta==200){
												console.log(d)
												uni.setStorageSync('userinfo', d.data)
												uni.switchTab({
													url: _this.router(`index`)
												})
												uni.showToast({
													title:'登录成功',
													icon:'none'
												})
											}else if(d.sta==2004){
											 uni.getUserInfo({
											      provider: 'weixin',
											      success: (infoRes)=>{
													  
													  let v = {
													  	encryptedData: infoRes.encryptedData,
													  	iv: infoRes.iv,
													  	session_key: userres.data.session_key,
													  	openid: userres.data.openid
													  }
													  _this.http('/api/user/getWxInfo', 'post', v,)
													  	.then((r) => {
													  		if(r.sta==200){
													  			_this.getnav(_this.router(`getPhone`));
													  		}else{
													  			_this.disabled = false;
													  			uni.showToast({
													  				title:r.msg,
													  				icon:'none'
													  			})
													  		}
													  	})
												  }
											    });
											}else{
												uni.showToast({
													title:d.msg,
													icon:'none'
												})
											}
										})
									
								})
						}
						_this.iv = e.detail.iv;
						_this.encryptedData = e.detail.encryptedData;
					},
					fail: () => {
								this.disabled = false;
								uni.showToast({
									title:'授权失败，请重新授权',
									icon:'none'
								})
							}
				});
			},
			login() {
				if (this.openid != '') {
					let v={
						openid:this.openid
					}
					this.http('/api/user/login', 'post',v , false)
						.then((res) => {
							uni.setStorageSync('userinfo', res.data)
						})
				}
			},
		}
	}
</script>

<style lang="less">
	.login {
		.title {
			padding: 80rpx 60rpx;
			font-size: 48rpx;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
		}

		.box {
			text-align: center;

			.logo {
				width: 240rpx;
				height: 240rpx;
			}
		}

		.btnBox {
			margin-top: 172rpx;
			padding: 0 32rpx;

			.loginBtn {
				width: 100%;
				height: 100rpx;
				color: #fff;
				font-size: 36rpx;
				line-height: 100rpx;
				background: rgba(104, 148, 185, 1) !important;
				border-radius: 5px;
			}

			.nologinBtn {
				margin-top: 52rpx;
				width: 100%;
				height: 100rpx;
				color: #333;
				font-size: 36rpx;
				line-height: 100rpx;
				background: #fff !important;
				border: 1px solid rgba(204, 204, 204, 1);
				border-radius: 5px;
			}
		}

		.boxes {
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>
